//
//  WeiboVisitorView.swift
//  SinaWeibo
//
//  Created by 风不会停息 on 2016/11/13.
//  Copyright © 2016年 风不会停息. All rights reserved.
//

import UIKit
import SnapKit

//
protocol WeiboVisitorViewDelegate :NSObjectProtocol {
    func userLogin()
    func userRegister()
}
class WeiboVisitorView: UIView {

    //代理需要使用weak，否则会出现循环引用！！！！！！
    weak var delegate:WeiboVisitorViewDelegate?
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
//        startAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        //背景颜色的设置
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    
        //添加圆环视图
        addSubview(circleView)
        //添加灰色遮盖
        addSubview(backView)
        //添加房子视图
        addSubview(houseView)
        //添加文本信息Label
        addSubview(textLabel)
        //添加登录和注册按钮
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-80)
            
        }
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        houseView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        textLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(20)
            make.centerX.equalTo(circleView)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(textLabel.snp.bottom)
            make.leading.equalTo(textLabel)
            make.width.equalTo(100)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(textLabel.snp.bottom)
            make.trailing.equalTo(textLabel)
            make.width.equalTo(100)
            
        }
        
        
    }
    //添加圆环的动画
    private func startAnimation(){
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        animator.toValue = M_PI * 2
        animator.repeatCount = MAXFLOAT
        animator.duration = 20
        //动画结束后不会移除动画
        animator.isRemovedOnCompletion = false
        circleView.layer.add(animator, forKey: "nil")
    }
    
    
    
    //懒加载圆环
    private lazy var circleView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //懒加载房子视图
    private lazy var houseView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //懒加载遮盖
    private lazy var backView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //添加文本框
    private lazy var textLabel:UILabel = {
        //自定义label的扩展方法，可以直接设置title fontSize和color
       let label = UILabel(title: "关注一些人,关注一些人,回到这里看看有什么惊喜", fontSize: 14, textcolor: UIColor.darkGray)
        //多行显示
        label.numberOfLines = 0
        //文字居中对齐
        label.textAlignment = NSTextAlignment.center
        //设置label的宽度时候用 这个方法！！！
        label.preferredMaxLayoutWidth = 220
        return label
    }()
    //懒加载登录和注册按钮
    private lazy var loginBtn :UIButton = {
       let btn = UIButton(title: "登录", fontSize: 14, textColor: UIColor.darkGray)
       btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        btn .addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        return btn
    }()
    private lazy var registerBtn :UIButton = {
        let btn = UIButton(title: "注册", fontSize: 14, textColor: UIColor.darkGray)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        btn .addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        return btn
    }()
    @objc private func loginClick(){
//        print(#function)
        delegate?.userLogin()
    }
    @objc private func registerClick(){
//        print(#function)
        delegate?.userRegister()
    }
    //对VisitorView进行封装
    func updateVisitorView(title:String,imageName:String,isHome:Bool)
    {
        self.textLabel.text = title
        self.circleView.image = UIImage(named: imageName)
        if isHome {
            startAnimation()
        }else{
            self.houseView.isHidden = true
            self.backView.isHidden = true
        }
    }
    

}
